package com.ruoyi.service;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.entity.MenuFast;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.vo.MenuFastVO;
import com.ruoyi.common.core.domain.vo.TodoVO;
import com.ruoyi.common.core.mapper.MenuFastMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.service.BaseServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mapper.SysDictDataMapper;
import com.ruoyi.mapper.SysMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 【请填写功能名称】
 * @date 2024-03-22
 */
@Service
public class MenuFastServiceImpl extends BaseServiceImpl<MenuFastMapper, MenuFast> {

    @Autowired
    private SysMenuMapper menuMapper;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysDictDataMapper dictDataMapper;


    public void selectPage(Page page, Map<String, Object> param) {
        baseMapper.selectPage(page, param);
    }

    /**
     * 更新点击次数
     */
    public void doSave(MenuFast menuFast) {
        LoginUser user = SecurityUtils.getLoginUserCanNotLogin();
        if (user == null || menuFast.getMenuId() == null) {
            return;
        }
        //每10次更新一次数据库
        String key = "menuFast:" + user.getUserId() + "-" + menuFast.getMenuId();
        if (redisCache.hasKey(key)) {
            Long times = redisCache.getCacheObject(key);
            times += 1L;
            redisCache.setCacheObject(key, times);
            if (times % 10 == 0) {
                updateClickTimes(menuFast, user, key, times);
            }
        } else {
            Long times = updateClickTimes(menuFast, user, key, null);
            redisCache.setCacheObject(key, times);
        }
    }

    private Long updateClickTimes(MenuFast menuFast, LoginUser user, String key, Long times) {
        SysMenu menu = menuMapper.selectById(menuFast.getMenuId());
        if (menu == null) {
            return 0L;
        }
        MenuFast old = baseMapper.getByMenuId(menuFast.getMenuId(), user.getUserId());
        if (old == null) {
            old = new MenuFast();
            old.setUserId(user.getUserId());
        }
        old.setMenuNickName(menu.getMenuNickName());
        old.setMenuName(menu.getMenuName());
        old.setMenuId(menuFast.getMenuId());
        if (times != null) {
            old.setClickTimes(times);
        } else {
            old.setClickTimes(old.getClickTimes() + 1);
        }

        old.insertOrUpdate();
        return old.getClickTimes();
    }

    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }


    /**
     * 获取首页数据
     */
    public Object getBackHomeData() {
        //获取当前用户待办任务
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysMenu> menuList = menuMapper.getAuthorityMenuList(user.getUserId());
        List<TodoVO> todoList = new ArrayList<>();
        addTodo(menuList, todoList, "专家待审核", "专家审核");
        addTodo(menuList, todoList, "活动待审核", "活动审核");

        //获取当前用户常用菜单
        List<MenuFastVO> menuFastList = new ArrayList<>();
        addFastMenu(menuList, menuFastList, "新建活动", "活动管理");
        addFastMenu(menuList, menuFastList, "专家管理", "专家管理");
        return MapUtil.builder().put("todoList", todoList).put("menuFastList", menuFastList).build();
    }

    /**
     * 添加常用菜单
     */
    private void addFastMenu(List<SysMenu> menuList, List<MenuFastVO> menuFastList, String title, String menuName) {
        SysMenu menu = menuList.stream().filter(r -> r.getComponent() != null && !r.getComponent().equals("Layout") && r.getMenuName().equals(menuName))
                .findAny().orElse(null);
        if (menu != null) {
            menuFastList.add(new MenuFastVO(title, menu));
        }
    }

    /**
     * 添加常用菜单 /#/hd/hdpj
     */
    private void addTodo(List<SysMenu> menuList, List<TodoVO> todoList, String title, String menuName) {
        SysMenu expertMenu = menuList.stream().filter(r -> r.getComponent() != null && r.getMenuName().equals(menuName))
                .findAny().orElse(null);
        if (expertMenu != null) {
            Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
            Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
            int count = dictDataMapper.getTodoCount(title, userId, deptId);
            if (count > 0) {
                todoList.add(new TodoVO(title, count, expertMenu));
            }
        }
    }
}
